/**
 * 
 */
package org.mips.Sirius.provider.SIRI;

import java.io.StringReader;
import java.util.ArrayList;

import org.mips.Sirius.components.Line;
import org.mips.Sirius.components.StopPoint;
import org.mips.Sirius.provider.ProviderException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

/**
 * 
 * @author Alessandro
 * 
 */
public class SIRIStopPointsDelivery {

	ArrayList<StopPoint> stopPoints;

	public SIRIStopPointsDelivery(String xml) throws ProviderException {

		parse(xml);
	}

	public ArrayList<StopPoint> getStopPoints() {
		return stopPoints;
	}

	private void parse(String xml) throws ProviderException {

		stopPoints = new ArrayList<StopPoint>();

		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(true);
			XmlPullParser xpp = factory.newPullParser();

			xpp.setInput(new StringReader(xml));
			int eventType = xpp.getEventType();

			String stopName = null;
			String stopRef = null;
			boolean openStopName = false;
			boolean openMonitored = false;
			boolean openStopRef = false;
			boolean openLineRef = false;
			ArrayList<Line> lines = new ArrayList<Line>();
			StopPoint stop = null;

			while (eventType != XmlPullParser.END_DOCUMENT) {
				if (eventType == XmlPullParser.START_TAG) {
					if (xpp.getName().equals("Lines")) {
						lines = new ArrayList<Line>();
					} else if (xpp.getName().equals("StopName")) {
						openStopName = true;
					} else if (xpp.getName().equals("Monitored")) {
						openMonitored = true;
					} else if (xpp.getName().equals("StopPointRef")) {
						openStopRef = true;
					} else if (xpp.getName().equals("LineRef")) {
						openLineRef = true;
					}
				} else if (eventType == XmlPullParser.END_TAG) {
					if (xpp.getName().equals("StopName")) {
						openStopName = false;
					} else if (xpp.getName().equals("Monitored")) {
						openMonitored = false;
					} else if (xpp.getName().equals("StopPointRef")) {
						openStopRef = false;
					} else if (xpp.getName().equals("LineRef")) {
						openLineRef = false;
					} else if (xpp.getName().equals("Lines")) {
						if (stop != null) {
							stop.setLines(lines);
							stopPoints.add(stop);
						}
					}
				} else if (eventType == XmlPullParser.TEXT) {
					if (openStopName) {
						stopName = xpp.getText();
					}
					if (openStopRef) {
						stopRef = xpp.getText();
					}
					if (openLineRef) {
						lines.add(new Line(xpp.getText()));
					}
					if (openMonitored) {
						if (xpp.getText().equals("true")) {
							if (stopRef != null && stopName != null) {
								stop = new StopPoint(stopRef, stopName);
							}
						}
					}
				}
				eventType = xpp.next();
			}
		} catch (Exception e) {
			throw new ProviderException(e, "Couldn't parse stopPointsDelivery");
		}

		Log.d("debug", "Found " + stopPoints.size() + " monitored stops: "
				+ stopPoints);
	}
}
